package hr.fer.zpr.nmbp.project2.model;

import hr.fer.zpr.nmbp.project2.model.util.DateUtil;
import java.io.Serializable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;

@Entity
@Table(name="DIJAGNOZA"
    ,schema="PUBLIC"
    ,catalog="EBOLNICA"
)
public class Dijagnoza implements Serializable {
    
    private String sifra;   //primarni ključ, umjetno generiran
    private Date datum;
    private String opis;
    
    private Karton karton;
    private List<Bolest> bolesti = new ArrayList<Bolest>();

    public Dijagnoza() {
    }
    public Dijagnoza(String sifra) {
        this.sifra = sifra;
    }
    public Dijagnoza(String sifra, Karton karton, Date datum, String opis, List bolesti) {
       this.sifra = sifra;
       this.karton = karton;
       this.datum = datum;
       this.opis = opis;
       this.bolesti = bolesti;
    }
    
    @Id
    @Column(name="SIFRA", unique=true, nullable=false)
    public String getSifra() {
        return sifra;
    }

    public void setSifra(String sifra) {
        this.sifra = sifra;
    }
    
    @ManyToMany(cascade=CascadeType.REFRESH, fetch=FetchType.LAZY)
    @JoinTable(name="DIJAGNOZA_BOLEST", schema="PUBLIC", catalog="EBOLNICA", joinColumns = { 
        @JoinColumn(name="DIJAGNOZA_SIFRA", updatable=false) }, inverseJoinColumns = { 
        @JoinColumn(name="BOLEST_SIFRA", updatable=false) })
    public List<Bolest> getBolesti() {
        return bolesti;
    }

    public void setBolesti(List<Bolest> bolesti) {
        this.bolesti = bolesti;
    }

    @Temporal(TemporalType.DATE)
    @Column(name="DATUM", length=8)
    public Date getDatum() {
        return datum;
    }

    public void setDatum(Date datum) {
        this.datum = datum;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="KARTON_PACIJENT_MBO")
    public Karton getKarton() {
        return karton;
    }

    public void setKarton(Karton karton) {
        this.karton = karton;
    }

    @Column(name="OPIS")
    public String getOpis() {
        return opis;
    }

    public void setOpis(String opis) {
        this.opis = opis;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Dijagnoza other = (Dijagnoza) obj;
        if ((this.sifra == null) ? (other.sifra != null) : !this.sifra.equals(other.sifra)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + (this.sifra != null ? this.sifra.hashCode() : 0);
        return hash;
    }
    
    @Override
    public String toString() {
    	return DateUtil.formatDate(this.datum) +"  "+ karton.getPacijent().toString();
    }    
}
